package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.House;
import com.hzdl.house_mgr.bean.Rent;
import com.hzdl.house_mgr.service.HouseService;
import com.hzdl.house_mgr.service.RentService;
import com.hzdl.house_mgr.service.UserService;
import com.hzdl.house_mgr.util.AjaxResult;
import com.hzdl.house_mgr.util.ExlExportUtil;
import com.hzdl.house_mgr.util.RentCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("business")
public class RentHouseController {
    
    @Autowired
    private RentService rentService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private UserService userService;

    /*@RequestMapping("rent/rentRightJson")
    public AjaxResult rentRightJson(Integer page, Integer limit){
        PageInfo<House> pageInfo = houseService.findAll(page, limit);
        Integer count = houseService.getCount();
        return AjaxResult.ok(count,pageInfo.getList());
    }*/

    /*@RequestMapping("rent/rentAdd")
    public AjaxResult rentAdd(Rent rent){
            int i = rentService.insertRent(rent);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
    }*/

    /*@RequestMapping("rent/checkUser")
    public AjaxResult checkUser(String idCard){
        int i = userService.findUserById(idCard);
        if(i>0){
            return AjaxResult.FIND_SUCCESS();
        }else{
            return AjaxResult.FIND_FAILED();
        }
    }*/

    /*@RequestMapping("rent/rentCodeJson")
    public String rentCodeJson(){
        String rentCode = "rent_";

        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd_HHmmss_SSS");
        String format = sdf.format(new Date());
        rentCode += format;

        rentCode += "-";

        Random random = new Random();
        for(int i = 0;i<5;i++){
            int i1 = random.nextInt(10);
            rentCode += i1;
        }
        return rentCode;
    }*/

    @RequestMapping("rentJson")
    public AjaxResult rentJson(Rent rent,Integer page,Integer limit){
        PageInfo<Rent> pageInfo = rentService.findAllByWhere(rent,page,limit);
        int count = rentService.getCountByWhere(rent);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    //生成订单
    @RequestMapping("getOrder")
    @ResponseBody
    public AjaxResult getOrder(Rent rent){
        //生成订单号
        String s = RentCodeUtil.rentCode();

        //通过rent.set订单号
        rent.setRentNum(s);
        int i = rentService.getRent(rent);
        if(i>0){
            return  AjaxResult.ok(s);
        }else {
            return  AjaxResult.ADD_FAILED();
        }
    }

    @RequestMapping("rentExport")
    public ResponseEntity<Object> rentExport(Integer rentId) throws UnsupportedEncodingException {
        System.out.println(rentId);
        // TODO 根据rentId查询出租单的信息

        ByteArrayOutputStream b = ExlExportUtil.readExcel("czd-templet.xls");
        // 导出文件名称
        String finleName = URLEncoder.encode("出租单信息.xls","UTF-8");

        // 创建响应头部信息
        HttpHeaders headers = new HttpHeaders();
        // 设置响应类型
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 设置下载文件的文件名称
        headers.setContentDispositionFormData("attachment",finleName);
        return new ResponseEntity<>(b.toByteArray(), headers, HttpStatus.CREATED);
    }
}
